package com.azzcs.elasticsearch.config;

import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author: wzg
 * @Date: 2021/5/14 上午7:35
 */
@Configuration
public class ElasticsearchConfig {
    @Value("${spring.elasticsearch.rest.uris}")
    private String hostList;

    @Bean
    public RestHighLevelClient client(){
        String[] hosts = hostList.split(",");
        HttpHost[] httpHost = new HttpHost[hosts.length];
        for (int i = 0; i < hosts.length; i++) {
            String host = hosts[i];
            httpHost[i] = new HttpHost(host.split(":")[0],
                    Integer.parseInt(host.split(":")[1]),"http");
        }
        RestHighLevelClient restHighLevelClient = new RestHighLevelClient(RestClient.builder(httpHost));
        return restHighLevelClient;
    }

}
